package tk1.uebung3.ws;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import org.jdesktop.application.Application;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ShopWSClient extends javax.swing.JFrame implements ActionListener {
	private JTextField product1TextField;
	private JLabel cartLabel;
	private JLabel product3Label;
	private JLabel product2Label;
	private JLabel product1Label;
	private JTextField product3TextField;
	private JLabel resultLabel;
	private JLabel product3CartLabel;
	private JLabel product2CartLabel;
	private JLabel product1CartLabel;
	private JButton sendButton;
	private JButton addToCartButton;
	private JTextField product2TextField;
		
	
	private JLabel[] productLabels;
	private JLabel[] productCartLabels;
	private JTextField[] productTextFields;
	
	private Shop shop;
	private String[] productNames;
	private String cartId;
	private HashMap<String, Integer> productTypesToAmounts;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ShopWSClient inst = new ShopWSClient();
				inst.setLocation(700, 300);
				inst.setVisible(true);
			}
		});
	}
	
	public ShopWSClient() {
		super();
		this.setTitle("SOAP Client");
		initGUI();
		this.productLabels = new JLabel[]{this.product1Label, this.product2Label, this.product3Label};
		this.productCartLabels = new JLabel[]{this.product1CartLabel, this.product2CartLabel, this.product3CartLabel};
		this.productTextFields = new JTextField[]{this.product1TextField, this.product2TextField, this.product3TextField};
		
		this.productTypesToAmounts = new HashMap<String,Integer>();
		
		ShopImplService service = new ShopImplService();
		this.shop = service.getShopImplPort();
		
		this.cartId = shop.getCartId();
		this.productNames = new String[3];
		int i = 0;
		for(String productName : shop.getProductTypes()){
			this.productNames[i] = productName;
			i++;
		}
		
		this.setProductLabels();
		this.setProductCartLabels();
		
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				product1TextField = new JTextField();
			}
			{
				product1Label = new JLabel();
				product1Label.setName("product1Label");
			}
			{
				product2Label = new JLabel();
				product2Label.setName("product2Label");
			}
			{
				product3Label = new JLabel();
				product3Label.setName("product3Label");
			}
			{
				cartLabel = new JLabel();
				cartLabel.setName("cartLabel");
				cartLabel.setText("Shopping Cart");
			}
			{
				product1CartLabel = new JLabel();
				product1CartLabel.setName("product1CartLabel");
			}
			{
				product2CartLabel = new JLabel();
				product2CartLabel.setName("product2CartLabel");
			}
			{
				product3CartLabel = new JLabel();
				product3CartLabel.setName("product3CartLabel");
			}
			{
				resultLabel = new JLabel();
				resultLabel.setName("resultLabel");
			}
			{
				product2TextField = new JTextField();
			}
			{
				product3TextField = new JTextField();
			}
			{
				addToCartButton = new JButton();
				addToCartButton.setName("addToCartButton");
				addToCartButton.setText("add to cart");
				addToCartButton.addActionListener(this);
			}
			{
				sendButton = new JButton();
				sendButton.setName("sendButton");
				sendButton.setText("send");
				sendButton.addActionListener(this);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(19, 19)
				.addComponent(cartLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addGap(28)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(product1TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product1Label, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product1CartLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
				.addGap(21)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(product2TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product2Label, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product2CartLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
				.addGap(21)
				.addGroup(thisLayout.createParallelGroup()
				    .addComponent(product3TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product3Label, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
				    .addComponent(product3CartLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
				.addGap(21)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(sendButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
				    .addComponent(addToCartButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE))
				.addGap(22)
				.addComponent(resultLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(40, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(product3Label, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
				                .addComponent(addToCartButton, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(product1Label, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE)
				                    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                        .addComponent(product2Label, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
				                        .addGap(7)))
				                .addGap(7)
				                .addGroup(thisLayout.createParallelGroup()
				                    .addComponent(product3TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(product2TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE)
				                    .addComponent(product1TextField, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
				                .addGap(21)))
				        .addGap(65)
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(product3CartLabel, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 14, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(product2CartLabel, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 21, Short.MAX_VALUE))
				            .addGroup(thisLayout.createSequentialGroup()
				                .addComponent(product1CartLabel, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 0, Short.MAX_VALUE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(cartLabel, 0, 108, Short.MAX_VALUE)
				                .addGap(32))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addGap(46)
				                .addComponent(sendButton, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
				                .addGap(0, 10, Short.MAX_VALUE))))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addPreferredGap(product3Label, resultLabel, LayoutStyle.ComponentPlacement.INDENT)
				        .addComponent(resultLabel, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)
				        .addGap(0, 0, Short.MAX_VALUE)))
				.addContainerGap(28, 28));
			pack();
			this.setSize(540, 328);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void setProductLabels(){
		int i = 0;
		for(String productName : this.productNames){
			if(i >= 3)
				break;
			String amount = String.valueOf(shop.getProductAmount(productName));
			String price = String.valueOf(shop.getProductPrice(productName));
			
			JLabel productLabel = this.productLabels[i];
			productLabel.setText(productName + " (" + amount + " x available) " + price + " Euro");
			i++;
		}
	}
	
	private void setProductCartLabels(){
		int i = 0;
		for(JLabel productCartLabel : this.productCartLabels){
			if(i>=3)
				break;
			
			int amount;
			try{
				amount = Integer.valueOf(productTextFields[i].getText());
			}
			catch(Exception e){
				amount = 0;
			}
			productCartLabel.setText(String.valueOf(amount) +" x " + productNames[i]);
			productTypesToAmounts.put(productNames[i], Integer.valueOf(amount));
			i++;
			
		}
	}
	
	private void sendPurchaseRequest(){
		List<String> unavailableProducts = new ArrayList<String>();
		boolean overallSuccess = true;
		boolean nothingInTheCart = true;
		for(String productName : productTypesToAmounts.keySet()){
			int amount = productTypesToAmounts.get(productName).intValue();
			if(amount != 0)
				nothingInTheCart = false;
			boolean success = shop.purchaseProduct(cartId, productName, amount);
			if(success == false){
				overallSuccess = false;
				unavailableProducts.add(productName);
			}
		}
		if(nothingInTheCart){
			this.resultLabel.setText("Please add some items to your shopping cart !!");
		}
		
		else if(overallSuccess){
			this.resultLabel.setText("Your purchases sent successfully to the server");
		}
		else{
			String warning = "Following items not available in the given amount: ";
			int i = 0;
			for(String unavailableProduct : unavailableProducts){
				if(i != 0)
					warning += ", ";
				warning += unavailableProduct;
				i++;
			}
			this.resultLabel.setText(warning);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == this.addToCartButton){
			this.setProductCartLabels();
		}
		else if(e.getSource() == this.sendButton){
			this.sendPurchaseRequest();
			this.setProductLabels();
		}
	}

}
